import { ConfigService } from '@nestjs/config';

export class ImageUtil {
  private static configService: ConfigService;

  static init(configService: ConfigService) {
    this.configService = configService;
  }

  static getFullUrl(path: string): string {
    if (!path) return '';
    if (path.startsWith('http')) return path;

    const endpoint = this.configService.get('MINIO_ENDPOINT', 'localhost');
    const port = this.configService.get('MINIO_PORT', '9000');
    const useSSL = this.configService.get('MINIO_USE_SSL', 'false') === 'true';
    const protocol = useSSL ? 'https' : 'http';

    return `${protocol}://${endpoint}:${port}/${path}`;
  }

  static getRelativePath(url: string): string {
    if (!url) return '';
    try {
      const urlObj = new URL(url);
      return urlObj.pathname.substring(1); // 移除开头的 '/'
    } catch {
      return url;
    }
  }
}
